package com.example.demo73es.controller;

import com.example.demo73es.entity.ArticleEntity;
import com.example.demo73es.reposity.ArticleReposity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;


@RestController
//@RequestMapping("/article")
public class EsArticleController {
//    @Resource(name="articleReposity")
//    private ArticleReposity articleReposity;


//    @Autowired
//    private  ArticleReposity articleReposity;

//    @Autowired
//    @Qualifier("articleReposity")
//    private  ArticleReposity articleReposity;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
//    @GetMapping("/findByTitle/{title}")
//    public List<ArticleEntity> findByTitle(@PathVariable String title){
//        List<ArticleEntity> byTitle = articleReposity.findByTitle(title);
//        return byTitle;
//    }
//
//    @PostMapping("/addArticle")
//    public ArticleEntity addArticle(@RequestBody ArticleEntity article) {
//        return articleReposity.save(article);
//    }
//
//    @GetMapping("/findArticle/{id}")
//    public Optional<ArticleEntity> findArticle(@PathVariable Long id) {
//        return articleReposity.findById(String.valueOf(id));
//    }
//
//    @DeleteMapping("/delete/{id}")
//    public void deleteArticle(@PathVariable Long id) {
//        articleReposity.deleteById(String.valueOf(id));
//    }


//    @GetMapping("/findByTitleOrContentPage")
//    public List<ArticleEntity> findByTitleOrContentPage(String title, String content, Integer pageNum, Integer pageSize) {
//        Pageable pageable = PageRequest.of(pageNum, pageSize);
//        return articleReposity.findByTitleOrContent(title, content, pageable);
//    }



//    @GetMapping("/findByTitleOrContentPageByTemplate/{title}/{content}/{pageNum}/{pageSize}")
//    public List<ArticleEntity> findByTitleOrContentPageByTemplate(@PathVariable String title,
//                                                                  @PathVariable String content,
//                                                                  @PathVariable Integer pageNum,
//                                                                  @PathVariable Integer pageSize)
//    {
//       NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
//                .withQuery(QueryBuilders.queryStringQuery(title).defaultField("title"))
//                .withQuery(QueryBuilders.queryStringQuery(content).defaultField("content"))
//                .withPageable(PageRequest.of(pageNum, pageSize))
//                .build();
//
//        return this.elasticsearchRestTemplate.queryForList(nativeSearchQuery, ArticleEntity.class);
//    }
    @GetMapping("/test")
    public void test(){
        boolean ex = this.elasticsearchRestTemplate.indexExists("index_blog1");
        System.out.println(ex);
    }

    @GetMapping("/test1")
    void testExist()  {
        boolean exists = elasticsearchRestTemplate.indexOps(IndexCoordinates.of("member_index")).exists();
        System.out.println(exists);
    }

    @GetMapping("/test2")
    void test2()  {
        System.out.println("exists");
    }

//    @PostMapping("/add")
//    public void add(@RequestBody ArticleEntity articleEntity){
//        ArticleEntity save = articleReposity.save(articleEntity);
//        System.out.println(save);
//    }
//
//   //测试： http://localhost:8080/article/findArticles/2/2
//
//    @GetMapping("/findArticles/{num}/{page_size}")
//    public Page<ArticleEntity> findAll(@PathVariable int num, @PathVariable int page_size){
//        //num从0开始
//        //页面大小为2的第3页
//        Page<ArticleEntity> articles = articleReposity.findAll(PageRequest.of(num-1, page_size));
//        return  articles;
//    }

}

